package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Employee;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavorMapper dishFlavorMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    RedisTemplate redisTemplate;


    /**
     * 分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult findDish(DishPageQueryDTO dishPageQueryDTO) {

        //1. 设置分页参数
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        //2.  执行查询
        Page<DishVO> p = dishMapper.findDish(dishPageQueryDTO);

        //3.  封装PageResult对象，包含查询的总记录数和查询结果
        PageResult pageResult = new PageResult(p.getTotal(), p.getResult());


        return pageResult;
    }

    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {
        //添加菜品数据到数据库
        Long createUser = BaseContext.getCurrentId();
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.addDish(dish);

        Long id = dish.getId();
        log.info("添加菜品成功{}", dish);

        //添加口味数据到数据库
        List<DishFlavor> dishFlavors = dishDTO.getFlavors();
        if (dishFlavors != null && dishFlavors.size() > 0) {
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setDishId(id);
                dishFlavorMapper.addDishFlavor(dishFlavor);
            }
        }
        log.info("添加口味成功");

    }

    /**
     * 根据菜品id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO findDishById(Long id) {
        DishVO dishVO = dishMapper.findDishById(id);
        List<DishFlavor> flavors=dishFlavorMapper.findDIshById(id);
        dishVO.setFlavors(flavors);
        log.info("查询id为{}的菜品{}",id,dishVO);
        return dishVO;
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     */
    @Override
    @Transactional
    public void changeDish(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        dishMapper.changeDish(dish);
        Long id =dishDTO.getId();

        List<DishFlavor> flavors=dishDTO.getFlavors();
        if(flavors !=null && flavors.size()>0){
            dishFlavorMapper.deleteFlavor(id);

            for (DishFlavor dishFlavor : flavors) {
                dishFlavor.setDishId(id);
                dishFlavorMapper.addDishFlavor(dishFlavor);
            }
        }

    }

    /**
     * 改变起售停售状态
     * @param status
     */
    @Override
    public void changeStatus(Integer status,Long id) {
        Dish dish=new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.changeDish(dish);
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void dropDish(List<Long> ids) {
        //删除菜品
        dishMapper.dropDish(ids);
        //删除菜品对应口味
        dishFlavorMapper.dropDishFlavor(ids);
    }

    /**
     * 根据分类id查询相关菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        List<Dish> dishes=dishMapper.findByCategoryId(categoryId);
        return dishes;
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {

        Long categoryId=dish.getCategoryId();
        String key = "dish_"+categoryId;

        List<DishVO> lists = (List<DishVO>) redisTemplate.opsForValue().get(key);

        if(lists != null && lists.size()>0){
            log.info("从redis中查询菜品数据");
            return lists;
        }else{
            List<Dish> dishList = dishMapper.list(dish);

            List<DishVO> dishVOList = new ArrayList<>();

            for (Dish d : dishList) {
                DishVO dishVO = new DishVO();
                BeanUtils.copyProperties(d,dishVO);

                //根据菜品id查询对应的口味
                List<DishFlavor> flavors = dishFlavorMapper.findDIshById(d.getId());

                dishVO.setFlavors(flavors);
                dishVOList.add(dishVO);
            }

            redisTemplate.opsForValue().set(key,dishVOList);

            return dishVOList;
        }

    }
}
